package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.entity.*;
import com.hrma.services.OvertimeApplicationService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/overtimeApplication")
@Slf4j
public class OvertimeApplicationController {
    private final OvertimeApplicationService overtimeApplicationService;

    public OvertimeApplicationController(OvertimeApplicationService overtimeApplicationService) {
        this.overtimeApplicationService = overtimeApplicationService;
    }

    @PostMapping("/add")
    public Result<Integer> add(@Validated @RequestBody OvertimeApplication data){
        log.debug("收到add请求，请求参数：{}", data);
        int overtimeCode = overtimeApplicationService.add(data);
        if(overtimeCode == 1){
            return Result.notOneDay(overtimeCode);
        }
        if(overtimeCode == 2){
            return Result.inWorkTime(overtimeCode);
        }
        return Result.ok(overtimeCode);
    }

    @PatchMapping("/update")
    public Result<?> update(@Validated @RequestBody OvertimeApplication data){
        log.debug("收到update请求，请求参数：{}", data);
        int overtimeCode = overtimeApplicationService.update(data);
        if(overtimeCode == 1){
            return Result.notOneDay(overtimeCode);
        }
        if(overtimeCode == 2){
            return Result.inWorkTime(overtimeCode);
        }
        return Result.ok(overtimeCode);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求！");
        return Result.ok(overtimeApplicationService.select());

    }

    @PostMapping("/list")
    public Result<ListWithCount<OvertimeApplicationVO>> getList(@RequestBody OvertimeApplicationDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<OvertimeApplicationVO> res = overtimeApplicationService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

    @PostMapping("/PersonalList")
    public Result<ListWithCount<OvertimeApplicationVO>> getPersonalList(@RequestBody OvertimeApplicationDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<OvertimeApplicationVO> res = overtimeApplicationService.getPersonalList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }
}
